﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace PS3Helpers.Helpers
{
    public enum ListWithChangedEventType { Insert, Remove, Add } // Clear,

    public class ListWithChangedEvent<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
    {
        private readonly List<T> _items = new List<T>();

        // An event that clients can use to be notified whenever the elements of the list change:
        public delegate void ChangedEventHandler(ListWithChangedEvent<T> sender, ListWithChangedEventType changetype, T element);
        public event ChangedEventHandler Changed;

        // Invoke the Changed event; called whenever list changes:
        protected virtual void OnChanged(ListWithChangedEventType changetype, T element)
        {
            if (Changed != null)
                Changed(this, changetype, element);
        }

        public void Clear()
        {
            // _items.Clear();
            // OnChanged(ListWithChangedEventType.Clear, default(T));

            // Make sure we send a "remove" event for every item in the list
            while (Count > 0)
            {
                RemoveAt(Count - 1);
            }
        }

        public T this[int index]
        {
            get { return _items[index]; }
            set
            {
                OnChanged(ListWithChangedEventType.Remove, _items[index]);
                _items[index] = value;
                OnChanged(ListWithChangedEventType.Insert, _items[index]);
            }
        }

        #region IList<T> Members

        public int IndexOf(T item)
        {
            return _items.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            _items.Insert(index, item);
            OnChanged(ListWithChangedEventType.Insert, item);
        }

        public void RemoveAt(int index)
        {
            T element = _items[index];
            _items.RemoveAt(index);
            OnChanged(ListWithChangedEventType.Remove, element);
        }

        T IList<T>.this[int index]
        {
            get { return _items[index]; }
            set
            {
                OnChanged(ListWithChangedEventType.Remove, _items[index]);
                _items[index] = value;
                OnChanged(ListWithChangedEventType.Insert, _items[index]);
            }
        }

        #endregion

        #region ICollection<T> Members

        public void Add(T item)
        {
            _items.Add(item);
            OnChanged(ListWithChangedEventType.Add, item);
        }

        public bool Contains(T item)
        {
            return _items.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            _items.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _items.Count; }
        }

        public bool IsReadOnly
        {
            get { return ((ICollection<T>)_items).IsReadOnly; }
        }

        public bool Remove(T item)
        {
            bool res = _items.Remove(item);
            OnChanged(ListWithChangedEventType.Remove, item);
            return res;
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return _items.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _items.GetEnumerator();
        }

        #endregion

        #region IList Members

        public int Add(object value)
        {
            int res = ((IList)_items).Add(value);
            OnChanged(ListWithChangedEventType.Add, default(T));
            return res;
        }

        public bool Contains(object value)
        {
            return ((IList)_items).Contains(value);
        }

        public int IndexOf(object value)
        {
            return ((IList)_items).IndexOf(value);
        }

        public void Insert(int index, object value)
        {
            ((IList)_items).Insert(index, value);
            OnChanged(ListWithChangedEventType.Insert, default(T));
        }

        public bool IsFixedSize
        {
            get { return ((IList)_items).IsFixedSize; }
        }

        public void Remove(object value)
        {
            ((IList)_items).Remove(value);
            OnChanged(ListWithChangedEventType.Remove, default(T));
        }

        object IList.this[int index]
        {
            get { return _items[index]; }
            set
            {
                OnChanged(ListWithChangedEventType.Remove, _items[index]);
                ((IList)_items)[index] = value;
                OnChanged(ListWithChangedEventType.Insert, _items[index]);
            }
        }

        #endregion IList Members

        #region ICollection Members

        public void CopyTo(Array array, int index)
        {
            ((ICollection)_items).CopyTo(array, index);
        }

        bool ICollection.IsSynchronized
        {
            get
            {
                return ((IList)_items).IsSynchronized;
            }
        }

        object ICollection.SyncRoot
        {
            get { return ((IList)_items).SyncRoot; }
        }

        #endregion ICollection Members
    }
}
